//
//  CapturePhotoTableViewCell.m
//  Rail
//
//  Created by hychen on 2018/5/1.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "CapturePhotoTableViewCell.h"

@interface CapturePhotoTableViewCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) CupturePhotoView *photoView;
@property (nonatomic, strong) UIImagePickerController *imagePicker;
@property (nonatomic, strong) NSMutableArray *selectPhotoArray;
@end
@implementation CapturePhotoTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.titleLabel = [UILabel labelWithText:@"" textColor:Color_RGB(44, 44, 44) font:[UIFont systemFontOfSize:18]];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
    }];
    
    self.photoView = [[CupturePhotoView alloc] init];
    self.photoView.maxSelectImageCount = 3;
    [self.contentView addSubview:self.photoView];
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(15);
    }];
    BMWeakSelf
    [self.photoView setBlockClickPhoto:^(CapturePhotoModel *model) {
        if (weakSelf.BlockClickPhoto) {
            weakSelf.BlockClickPhoto(model);
        }
    }];
    
    [self.photoView setBlockClickDeletePhoto:^(NSIndexPath *indexPath) {
        [weakSelf deletePhotoWithIndexPath:indexPath];
    }];
}

- (void)deletePhotoWithIndexPath:(NSIndexPath *)indexPath {
    // 移除选中图片数据
    if (indexPath.row >= self.selectPhotoArray.count) {
        return;
    }
    [self.selectPhotoArray removeObjectAtIndex:indexPath.row];
    
    // 更新选中相片布局
    self.photoView.selectedImageArray = self.selectPhotoArray;
    if (self.BlockSelectPhotoArray) {
        self.BlockSelectPhotoArray(self.selectPhotoArray);
    }
}

- (void)setPhotoArray:(NSMutableArray *)selectPhotoArray {
    self.selectPhotoArray = selectPhotoArray;
    self.photoView.selectedImageArray = selectPhotoArray;
}

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}
@end
